<template>
  <div>
    <teleport to="body">
      <transition name="fade">
        <!-- bg-zinc-900/80 /80表示透明度 -->
        <div
          v-if="isVisible"
          class="w-screen h-screen bg-zinc-900/80 z-40 fixed top-0 left-0"
          @click="isVisible = false"
        ></div>
      </transition>
      <transition name="popup-down-up">
        <!-- v-bind="$attrs" 在使用这个组件的过程中 父组件指定的一些属性会绑定到上面 -->
        <div
          v-if="isVisible"
          v-bind="$attrs"
          class="w-screen dark:bg-zinc-800 bg-white z-50 fixed bottom-0"
        >
          <slot></slot>
        </div>
      </transition>
    </teleport>
  </div>
</template>
<script setup>
import { useScrollLock, useVModel } from '@vueuse/core'
import { watch } from 'vue'
const props = defineProps({
  modelValue: {
    required: true,
    type: Boolean
  }
})
defineEmits(['update:modelValue'])
// 返回响应式的数据 当 isVisible 值发生变化时 会自动触发emit 修改modelValue
const isVisible = useVModel(props)
// 滚动锁定
const isLocked = useScrollLock(document.body)

watch(
  isVisible,
  (val) => {
    isLocked.value = val
  },
  { immediate: true }
)
</script>

<style scoped lang="scss">
// 过度开始，过度离开
.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s;
}
// 准备进入，离开完成
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
.popup-down-up-enter-active,
.popup-down-up-leave-active {
  transition: all 0.3s;
}
.popup-down-up-enter-from,
.popup-down-up-leave-to {
  transform: translateY(100%);
}
</style>
